package com.ezops.poc.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Scanner;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.xml.sax.SAXException;

import com.ezops.annotation.EzopsDisplayName;
import com.ezops.model.EzopsOTModel;

public class EZOPSQueryGenerator {
	static String FIELD_INSERTION_QUERY = "INSERT INTO EZOPSDB.EZOPS_FIELDS(FIELD_NAME,TYPE,ASSET_ID) VALUES ('%s','NA',%d);";
	static String CLIENT_FIELD_INSERTION_QUERY = "INSERT INTO EZOPSDB.CLIENT_FIELDS(FIELD_NAME,CLIENT_ID,ASSET_ID) VALUES ('%s',1,%d);";
	static String MAPPING_INSERTON_QUERY = "INSERT INTO EZOPSDB.EZOPS_CLIENT_FIELD_MAPPING(EZOPS_FIELD_ID,CLIENT_FIELD_ID)  SELECT A.FIELD_ID, B.FIELD_ID FROM EZOPSDB.EZOPS_FIELDS A,  EZOPSDB.CLIENT_FIELDS B  WHERE lower(A.FIELD_NAME) LIKE '%s'  AND lower(B.FIELD_NAME) LIKE '%s' ;";
	

	
	public static void main(String[] args) throws SAXException, IOException,
			ParserConfigurationException, TransformerException {
	//	generateTempQuery();
		String option = args[0];
		switch (option) {
		
		case "ezopsFields":
			generateInsertionForEzopsFields();
			break;
		case "clientFields":
			generateInsertionForClientMapping();
			break;

		case "mapping":
			generateInsertionMapping();
			break;
		}

	}

	

	private static void generateInsertionMapping() {
		try {
			Scanner scannerclient = new Scanner(new File(
					"CITI_OT_EQUITY_Client_Mapping.csv"));
			Scanner scannerezops = new Scanner(new File(
					"CITI_OT_EQUITY_Client_Mapping.csv"));
			scannerclient.useDelimiter(",");
			scannerezops.useDelimiter(",");
			scannerezops.nextLine();
			String[] ezopsFields = scannerezops.nextLine().split(",");
			int i = -1;
			while (scannerclient.hasNext()) {
				System.out.println(String.format(MAPPING_INSERTON_QUERY,
						scannerclient.next(), ezopsFields[++i]));

			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	private static void generateInsertionForEzopsFields() {
		
			Field[] fields = EzopsOTModel.class.getDeclaredFields();
			for(Field field : fields){
				
				EzopsDisplayName displayName = field.getAnnotation(EzopsDisplayName.class);
				if(displayName == null) continue;
				System.out.println(String.format(FIELD_INSERTION_QUERY,
						displayName.name(), 3));
				
			}
		

		
	}

	private static void generateInsertionForClientMapping() {
		try {
			Scanner scanner = new Scanner(new File("CITI_OT_EQUITY_Test.csv"));
			//scanner.useDelimiter(",");
			while (scanner.hasNextLine()) {
				System.out.println(String.format(CLIENT_FIELD_INSERTION_QUERY,
						scanner.nextLine().trim(), 3));

			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

//	private static void generateInsertionQueryForFields(String fileName,
//			int assetType) throws ParserConfigurationException, SAXException,
//			IOException {
//		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
//				.newInstance();
//		DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
//		Document document = docBuilder.parse(new File(fileName));
//		parse(document.getDocumentElement(), assetType);
//	}
//
//	public static void parse(Node node, int assetType) {
//		// do something with the current node instead of System.out
//		System.out.println(String.format(FIELD_INSERTION_QUERY,
//				node.getNodeName(), assetType));
//
//		NodeList nodeList = node.getChildNodes();
//		for (int i = 0; i < nodeList.getLength(); i++) {
//			Node currentNode = nodeList.item(i);
//			if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
//				// calls this method for all the children which is Element
//				parse(currentNode, assetType);
//			}
//		}
//	}
}
